package com.wrupple.muba.catalogs.server.service.impl;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.wrupple.muba.catalogs.server.integration.handlers.TypedCatalogRequestHandler;
import com.wrupple.muba.catalogs.server.service.CatalogTokenInterpret;
import com.wrupple.muba.catalogs.server.service.CatalogTokenMapper;
import com.wrupple.vegetate.domain.CatalogObject;
import com.wrupple.vegetate.domain.exceptions.DataLayerException;
import com.wrupple.vegetate.server.domain.UnmutableModuleMap;
import com.wrupple.vegetate.server.integration.HttpServletRequestHandler;

public class CatalogTokenMapperImpl extends UnmutableModuleMap implements
		CatalogTokenMapper {

	private static final long serialVersionUID = 3073027354389841677L;
	Provider<TypedCatalogRequestHandler> genericHandler;
	CatalogTokenInterpret interpret;
	
	@Inject
	public CatalogTokenMapperImpl(
			Provider<TypedCatalogRequestHandler> genericHandler,CatalogTokenInterpret interpret) {
		super();
		this.genericHandler = genericHandler;
		this.interpret=interpret;
	}



	@Override
	public Provider<? extends HttpServletRequestHandler> get(Object arg0) {
		String catalogid = (String) arg0;
		Provider<? extends HttpServletRequestHandler> handler = super.get(catalogid);
		if(handler==null){
			Class<? extends CatalogObject> clazz;
			try {
				clazz = interpret.interpretCatalogNameAsJavaClass(catalogid);
			} catch (DataLayerException e) {
				clazz=null;
			}
			assert clazz!=null;
			handler = genericHandler;
			put(catalogid, handler);
		}
		return handler;
	}

}
